<? 
	include('base.php');
	User::protect();
	include_class('general_media');
	include_class('m2');
	$editors = array('description');

	$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
	$gm = new GeneralMedia();
	if ($id != 0) {
		$ma = MediaArea::get($id);
		if (!db::isError($ma)) {
			$gm->setAreaObject($ma);
			$isNotGeneralMedia = !$ma->isGeneralMedia();
		}
		
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi)) {
			switch($_GET['task']) {
				case 'update':
					$res = $mi->update($_POST, $gm);
					if (!db::isError($res)) {
						header('Location: media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					}
					break;
				case 'deactivate':
					$res = $mi->deactivate($gm);
					if (!db::isError($res)) {
						header('Location: media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					} 
					break;
				case 'activate':
					$res = $mi->activate($gm);
					if (!db::isError($res)) {
						header('Location: media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					}
					break;
				case 'delete':
					$res = $mi->delete($gm);
					if (!db::isError($res)) {
						header('Location: media.php?id=' . $_GET['id']);
					}
					break;
			}
		}
	}
		
	$section = 'media';
	$page_title = 'Edit Media';
	include('layout/header.php');
	
?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="media.php">Media&nbsp;&#62;</a>
<? 
	if (is_object($ma) && !db::isError($ma)) {
		$breadcrumb = $ma->getAreaTrail();
		if ($breadcrumb) { 
			foreach($breadcrumb as $maBC) {
				echo('<a href="media.php?id=' . $maBC->getID() . '">' . $maBC->getTitle() . '&nbsp;&#62;&nbsp;</a>');
			}
		}
	}	
	echo("View/Edit Media");
?>
</div>

<? if ($isNotGeneralMedia) {

	Error::outputDialog('Return to Media','media.php', 'You may only edit general media here. Any media that has been added to a specific band member, show or release must be edited in its appropriate section.');

} else if (!db::isError($ma)) { 
				
	if (!db::isError($mi)) { 
	
		if (db::isError($res)) { 
			$res->outputList();
		}
		
		if ($_GET['task'] == 'edit' && User::isAdmin()) {
			$canEdit = true;
			$exitURL = 'media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' .  $_GET['media_instance_id'];
			
			include('layout/media_edit.php');
		} else {
			$exitURL = 'media.php?id=' . $_GET['id'];
			if (User::isAdmin()) { 
				$canEdit = true;
				$editURL = 'media_edit.php?task=edit&id=' . $_GET['id'] . '&media_instance_id=' .  $_GET['media_instance_id'];
			}
			include('layout/media_view.php');
		}
	} else {
		$mi->outputDialog('Return to Media', 'media.php?id=' . $ma->getID());
	}
} else {
	$ma->outputDialog('Return to Media','media.php');
}

include('layout/footer.php'); ?>
